package org.jypj.dev.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.jypj.dev.model.Road;
import org.jypj.dev.model.Train;
import org.jypj.dev.service.IRoadService;
import org.jypj.dev.service.ITrainService;
import org.jypj.dev.util.HttpUtils;
import org.jypj.dev.util.PropertyUtil;
import org.jypj.dev.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * Road控制器
 * 
 * @author ChenYu
 *
 */
@Controller
@RequestMapping("roadController")
public class RoadController {

	@Resource
	private IRoadService roadService;

	@Resource
	private ITrainService trainService;

	/**
	 * 跳转交路管理首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "toRoadList.action")
	public String toRoadList() {

		return "back/bus/road_main";
	}

	/**
	 * 表单页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "toRoadForm.action")
	public String toRoadForm(String id, HttpServletRequest request) {
		if (StringUtil.isNotEmpty(id)) {
			Road road = roadService.selectRoadById(id);
			request.setAttribute("road", road);
		}
		return "back/bus/road_form";
	}

	/**
	 * 跳转选择车次列表也
	 * 
	 * @return
	 */
	@RequestMapping(value = "toRoadTrainList.action")
	public String toRoadTrainList(String roadId, HttpServletRequest request) {

		request.setAttribute("roadId", roadId);// 设置选中的roadid

		return "back/bus/road_train_list";
	}
	/**
	 * 查询左侧交路
	 * 
	 * @return
	 */
	@RequestMapping(value = "selectAllTreeData.action")
	@ResponseBody
	public Map<String, Object> selectAllTreeData(String searchKey,String tempTrain) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> queryMap = new HashMap<>();
		int code = 0;
		try {
			queryMap.put("searchKey", searchKey);
			queryMap.put("tempTrain",tempTrain);
			List<Road> roads = roadService.selectAllTreeData(queryMap);
			for (Road road : roads) {
				if(road.getNum().equals("1")){
					road.setPid("wuhan");
				}
				if(road.getNum().equals("0")){
					road.setPid("hankou");
				}
			}
			Road road1 =new Road("wuhan","武汉交路","root");
			Road road2 =new Road("hankou","汉口交路","root");
			Road root =new Road("root","交路管理","0");
			roads.add(road1);
			roads.add(road2);
			roads.add(root);
			map.put("datas", roads);
		} catch (Exception e) {
			code = -1;
		}
		map.put("code", code);
		return map;

	}

	/**
	 * 查询所有数据接口
	 * 
	 * @return code =-1 表示有异常 code=-2 表示没有数据 datas 菜单数据
	 */
	@RequestMapping("selectAllRoad.action")
	@ResponseBody
	public Map<String, Object> selectAllRoad(String roadId, String searchKey,String num,String tempTrain) {
		Map<String, Object> map = new HashMap<>();
		Map<String, Object> queryMap = new HashMap<>();
		int code = 0;
		try {
			queryMap.put("roadId", roadId);
			queryMap.put("searchKey", searchKey);
			queryMap.put("num", num);
			queryMap.put("tempTrain",tempTrain);
			List<Road> roads = roadService.selectAllByMap(queryMap);
			map.put("datas", roads);
		} catch (Exception e) {
			code = -1;
		}
		map.put("code", code);
		return map;
	}

	/**
	 * 从交路中移除车次
	 * 
	 * @return
	 */
	@RequestMapping("removeTrainForRoad.action")
	@ResponseBody
	public Map<String, Object> removeTrainForRoad(String id) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			Train train = new Train();
			train.setId(id);
			train.setRoadId("");
			trainService.updateTrainByField(train);
		} catch (Exception e) {
			e.printStackTrace();
			code = -1;
		}
		map.put("code", code);
		return map;

	}

	/**
	 * 在交路中添加一个个车次
	 * 
	 * @return
	 */
	@RequestMapping("addTrainInRoad.action")
	@ResponseBody
	public Map<String, Object> addTrainInRoad(String roadId, String ids) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		List<Train> trains = new ArrayList<>();
		if (StringUtil.isNotEmpty(ids)) {
			String[] split = ids.split(",");
			for (String id : split) {
				Train train = new Train();
				train.setId(id);
				train.setRoadId(roadId);
				trains.add(train);
			}
		}
		code = trainService.updateAllTrain(trains);
		map.put("code", code);
		return map;

	}

	/**
	 * 新增交路
	 * 
	 * @param road
	 * @return
	 */
	@RequestMapping("saveOrUpdateRoad.action")
	@ResponseBody
	public Map<String, Object> saveOrUpdateRoad(Road road) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			if (StringUtil.isNotEmpty(road.getId())) {
				road.setPid("root");
				roadService.updateRoad(road);
			} else {
				road.setPid("root");
				roadService.saveRoad(road);
			}
		} catch (Exception e) {
			e.printStackTrace();
			code = -1;
		}
		map.put("code", code);
		return map;
	}


	/**
	 * 删除交路
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping("deleteRoad.action")
	@ResponseBody
	public Map<String, Object> deleteRoad(String id) {
		Map<String, Object> map = new HashMap<>();
		int code = 0;
		try {
			roadService.deleteRoadById(id);
		} catch (Exception e) {
			e.printStackTrace();
			code = -1;
		}
		map.put("code", code);
		return map;
	}
}